﻿using Microsoft.Extensions.DependencyInjection;
using OpenIddict.Abstractions;
using static OpenIddict.Abstractions.OpenIddictConstants;
using static OpenIddict.Abstractions.OpenIddictConstants.Requirements;

namespace WT.OpenIddict.Repository.Seed;

public class ApplicationProvider : SeedProvider
{
    private readonly IOpenIddictApplicationManager _applicationManager;

    public ApplicationProvider(IServiceProvider serviceProvider) : base(serviceProvider)
    {
        _applicationManager = serviceProvider.GetRequiredService<IOpenIddictApplicationManager>();
    }

    public override async Task InitSeedData()
    {
        var application = await _applicationManager.FindByClientIdAsync("IdentityServer_Client");
        if (application != null) return;
        await _applicationManager.CreateAsync(new OpenIddictApplicationDescriptor
        {
            ClientId = "IdentityServer_Client",
            ClientSecret = "1q2w3E*",
            DisplayName = "认证测试应用",
            RedirectUris =
            {
                new Uri("http://localhost:8080/oauth2/callback") // 添加多个 URI 可继续逗号分隔
            },
            PostLogoutRedirectUris =
            {
                new Uri("http://localhost:8080/oauth2") // 添加多个 URI 可继续逗号分隔
            },
            ApplicationType = ApplicationTypes.Web,
            ClientType = ClientTypes.Confidential,
            ConsentType = ConsentTypes.Explicit,
            Requirements = { Features.ProofKeyForCodeExchange },
            Permissions =
            {
                Permissions.Endpoints.Token,
                Permissions.Endpoints.Revocation,
                Permissions.Endpoints.Introspection,
                Permissions.Endpoints.Logout,
                Permissions.GrantTypes.Password,
                Permissions.Endpoints.Authorization,
                Permissions.GrantTypes.AuthorizationCode,
                Permissions.GrantTypes.ClientCredentials,
                Permissions.Scopes.Roles,
                Permissions.Scopes.Email,
                Permissions.Scopes.Profile,
                Permissions.ResponseTypes.Code,
                Permissions.GrantTypes.RefreshToken
            }
        });
        await _applicationManager.CreateAsync(new OpenIddictApplicationDescriptor
        {
            ClientId = "Test_OpenIddict",
            ClientSecret = "1q2w3E*",
            DisplayName = "第一个应用",
            RedirectUris =
            {
                new Uri("http://localhost:8081/callback") // 添加多个 URI 可继续逗号分隔
            },
            PostLogoutRedirectUris =
            {
                new Uri("http://localhost:8081/") // 添加多个 URI 可继续逗号分隔
            },
            ApplicationType = ApplicationTypes.Web,
            ClientType = ClientTypes.Confidential,
            ConsentType = ConsentTypes.Explicit,
            Requirements = { Features.ProofKeyForCodeExchange },
            Permissions =
            {
                Permissions.Endpoints.Token,
                Permissions.Endpoints.Revocation,
                Permissions.Endpoints.Introspection,
                Permissions.Endpoints.Logout,
                Permissions.GrantTypes.Password,
                Permissions.Endpoints.Authorization,
                Permissions.GrantTypes.AuthorizationCode,
                Permissions.GrantTypes.ClientCredentials,
                Permissions.Scopes.Roles,
                Permissions.Scopes.Email,
                Permissions.Scopes.Profile,
                Permissions.ResponseTypes.Code,
                Permissions.GrantTypes.RefreshToken
            }
        });
    }
}